package com.example.changtest.ui

import android.annotation.SuppressLint
import android.content.Context
import android.os.Bundle
import android.os.PowerManager
import androidx.appcompat.app.AppCompatActivity
import com.example.changtest.R

class BatteryActivity : AppCompatActivity() {
    var wakeLock : PowerManager.WakeLock? = null


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_battery)

        //... do work...

    }

    @SuppressLint("InvalidWakeLockTag")
    override fun onResume() {
        val pm :PowerManager = this.getSystemService(Context.POWER_SERVICE) as PowerManager
        wakeLock = pm.newWakeLock(PowerManager.ON_AFTER_RELEASE or PowerManager.PARTIAL_WAKE_LOCK, "Tag")

        //申请WakeLock
        wakeLock?.acquire()
        super.onResume()
    }

    override fun onPause() {
        // 释放wakeLock
        // 当使用wakeLock.acquire(timeout)的方式时系统会自动释放
        wakeLock?.release()
        super.onPause()
    }


}